﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class Admin_Components_MasterData_Introduction : System.Web.UI.UserControl
{
    DBDataContext db;
    //DatabaseHelper helper;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            loadData(cbType.SelectedValue.Trim());
    }
    private void loadData(string  strType)
    { 
         db = new DBDataContext();
         var list = (from a in db.Introductions
                     where a.Status == 'A'
                           && a.Type == strType
                     select a).FirstOrDefault();
         if (list != null)
         {
             RadEditorContent.Content = list.Content;
             txtLastUpdatedBy.Text = list.LastUpdatedBy;
             txtLastUpdatedDateTime.Text = string.Format("{0:dd/MM/yyyy}", list.LastUpdatedDateTime);
             ViewState["IntroductionID"] = list.ID;
         }
         else
         {
             RadEditorContent.Content = "";
             txtLastUpdatedBy.Text = "";
             txtLastUpdatedDateTime.Text = "";
             ViewState["IntroductionID"] = null;
         }
         db.Connection.Close();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["Username"] != null)
        {
            try {
                if (string.IsNullOrEmpty(cbType.SelectedValue.Trim()))
                {
                    JavascriptUtil.LoadMessage(UpdatePanel1, false, "Vui lòng chọn loại!");
                    cbType.Focus();
                    return;
                }
                if (String.IsNullOrEmpty(RadEditorContent.Content.Trim()))
                {
                    JavascriptUtil.LoadMessage(UpdatePanel1, false, "Vui lòng nhập nội dung chi tiết!");
                    RadEditorContent.Focus();
                    return;
                }
                db = new DBDataContext();
                if (ViewState["IntroductionID"] == null)
                {
                    Introduction obj = new Introduction();
                    obj.Content = RadEditorContent.Content.Trim();
                    obj.Type = Utils.KillChars(cbType.SelectedValue);
                    obj.CreatedBy = Session["Username"].ToString();
                    obj.CreatedDateTime = DateTime.Now;
                    obj.LastUpdatedBy = Session["Username"].ToString();
                    obj.LastUpdatedDateTime = DateTime.Now;
                    obj.Status = 'A';
                    db.Insert<Introduction>(obj);
                    db.SubmitChanges();
                    ViewState["IntroductionID"] = obj.ID;
                }
                else
                {
                   Introduction objSelect = db.Introductions.FirstOrDefault(x => x.ID == Convert.ToInt32(ViewState["IntroductionID"]) && x.Type ==  Utils.KillChars(cbType.SelectedValue));
                   if (objSelect != null)
                   {
                       objSelect.Content = RadEditorContent.Content.Trim();
                       objSelect.LastUpdatedBy = Session["Username"].ToString();
                       objSelect.LastUpdatedDateTime = DateTime.Now;
                       db.SubmitChanges();
                   }
                   //else
                   //{
                   //    Introduction obj = new Introduction();
                   //    obj.Content = RadEditorContent.Content.Trim();
                   //    obj.Type = Utils.KillChars(cbType.SelectedValue);
                   //    obj.CreatedBy = Session["Username"].ToString();
                   //    obj.CreatedDateTime = DateTime.Now;
                   //    obj.LastUpdatedBy = Session["Username"].ToString();
                   //    obj.LastUpdatedDateTime = DateTime.Now;
                   //    obj.Status = 'A';
                   //    db.Insert<Introduction>(obj);
                   //    db.SubmitChanges();
                   //    ViewState["IntroductionID"] = obj.ID;
                   //}

                }
                JavascriptUtil.LoadMessage(UpdatePanel1, true, "Lưu thành công!");
            }
            catch(Exception ex) {
                JavascriptUtil.LoadMessage(UpdatePanel1, false, "Thông báo lỗi: Lưu không thành công, vui lòng kiểm tra dữ liệu!");
                ControlUtil.LogError("Introduction Page-Save:" + ex.Message);
            }
        }
    }
    protected void cbType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        if (!string.IsNullOrEmpty(cbType.SelectedValue.Trim()))
        {
            loadData(cbType.SelectedValue.Trim());
        }
    }
}